/**
* Copyright 2013 nabla
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*/
package com.nabla.dc.server.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.nabla.wapp.server.database.Database;
import com.nabla.wapp.server.database.SqlProperties;
import com.nabla.wapp.server.database.StatementFormat;

public class MySqlResource {

	public static final String			SQL_RESOURCE_FILE = "my_sql_statement.xml";

	private static final SqlProperties	sql = new SqlProperties(MySqlResource.class.getResourceAsStream(SQL_RESOURCE_FILE));

	public static String load(final String name) {
		return sql.load(name);
	}

	public static ResultSet execute(final Statement stmt, final String sql) throws SQLException {
		return stmt.executeQuery(load(sql));
	}

	public static boolean execute(final Connection conn, final String sql, final Object... parameters) throws SQLException {
		return Database.executeUpdate(conn, load(sql), parameters);
	}

	public static PreparedStatement prepare(final Connection conn, final String sql, Object... parameters) throws SQLException {
		return StatementFormat.prepare(conn, Statement.NO_GENERATED_KEYS, load(sql), parameters);
	}

	public static PreparedStatement prepare(final Connection conn, int autoGeneratedKeys, final String sql, Object... parameters) throws SQLException {
		return StatementFormat.prepare(conn, autoGeneratedKeys, load(sql), parameters);
	}

}
